#ifndef __CCU_H__
#define __CCU_H__

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "list.h"
#include "lock.h"
#include "wait.h"
#include <sys/prctl.h>
#include <signal.h>
#include <time.h>
#include "elog.h"

#ifdef __cplusplus
extern "C"
{
#endif

#include "uconfig.h"


#define MAX_UART_MESSAGE_LENGTH 1400
#define MAX_CCU_PACKAGE_NUM 32
#define MAX_CCU_AI_INFO_CACHE_NUM 128

struct ccu_frame{
    struct list_head head;
    uint8_t buffer[MAX_UART_MESSAGE_LENGTH];
    uint32_t length;
};

struct ccu;

enum CCU_EVENT {
    CCU_EVENT_NONE = 0,
    CCU_EVENT_RECV,
    CCU_EVENT_MAX,
};

typedef int32_t (*ccu_event_notify)(struct ccu*, int32_t, void*, void*);

struct ccu_event_action {
    ccu_event_notify notify;
    void* object;
    struct ccu_event_action* next;
};

struct ccu_operation {
    int32_t (*init)(struct ccu*);
    int32_t (*release)(struct ccu*);

    int32_t (*start)(struct ccu*);
    int32_t (*stop)(struct ccu*);

    int32_t (*handle)(struct ccu *pccu);

    int32_t (*put_frame)(struct ccu*, struct list_head*, struct ccu_frame*);
	struct ccu_frame* (*get_frame)(struct ccu*, struct list_head*);

    int32_t (*create_uconfig)(struct ccu*);
    int32_t (*destroy_uconfig)(struct ccu*);
    int32_t (*create_threadpool)(struct ccu*);
    int32_t (*destroy_threadpool)(struct ccu*);

    int32_t (*register_notify)(struct ccu*, int32_t, ccu_event_notify notify, void*);
    int32_t (*unregister_notify)(struct ccu*, int32_t, void*);
    int32_t (*trigger_notify)(struct ccu*, int32_t, void*);
};

struct ccu {
    struct list_head head;
    wait_t wait;
    lock_t lock;
    lock_t notify_lock;
    int running_flag;
	pthread_t pid;
    int enable;

    struct ccu_operation* op;
    struct ccu_event_action *paction[CCU_EVENT_MAX];

    uint32_t timer_counter;
    uint8_t timer_1hz_over;
    /************************************队列管理**********************************/

    /************************************子模块**********************************/
    /* uconfig */
    struct uconfig *puconfig;
    /* threadpool */
    struct threadpool *pthreadpool;

    /************************************输入数据缓存**********************************/
    /* input cache */

    /************************************输出数据缓存**********************************/
    /* output cache */
};

int32_t create_init_ccu(struct ccu** pccu);
void release_destroy_ccu(struct ccu* pccu);


#ifdef __cplusplus
}
#endif    
#endif


